package com.mdb.open.trade.resource;

import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.api.enums.ApiEnum;
import com.mdb.bc.api.enums.OrderByEnum;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.bc.web.support.SuperResources;
import com.mdb.open.common.enums.trade.type.MerchantTypeEnum;
import com.mdb.open.trade.domain.dto.product.QueryMerchantLimitDto;
import com.mdb.open.trade.domain.dto.product.QueryMerchantProdLimitPageDto;
import com.mdb.open.trade.domain.po.trade.MerchantInfo;
import com.mdb.open.trade.domain.vo.product.CustProdVo;
import com.mdb.open.trade.domain.vo.product.CustomerLimitVo;
import com.mdb.open.trade.domain.vo.product.LimitUserProdVo;
import com.mdb.open.trade.domain.vo.product.MerchantLimitVo;
import com.mdb.open.trade.facade.LimitFacade;
import com.mdb.open.trade.facade.ProdFacade;
import com.mdb.open.trade.service.trade.TfMerchantInfoService;
import com.mdb.user.LoginUserInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 类描述：
 *  产品基础信息表 前端控制器
 *
 * @author liuyi
 * @date 2019-07-08
 */
@RestController
@RequestMapping("product")
@Api(tags = "产品信息")
public class ProductResource extends SuperResources {

    @Autowired
    private LimitFacade limitFacade;

    @Autowired
    private LoginUserInfoHelper loginUserInfoHelper;

    @Autowired
    private TfMerchantInfoService merchantInfoService;

    @Autowired
    private ProdFacade prodFacade;

    @LogAnnotation
    @ApiOperation("客户已开通产品列表")
    @GetMapping("/list")
    public ListResultEx<CustProdVo> queryCustProdList() {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        return prodFacade.queryCustProdList(customerCode);
    }

    @LogAnnotation
    @ApiOperation("商户产品额度列表")
    @GetMapping("/merchant_limit_page_list")
    public ListResultEx<MerchantLimitVo> queryMerchantLimitPageList(@ModelAttribute QueryMerchantLimitDto dto) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);

        QueryMerchantProdLimitPageDto pageDto = new QueryMerchantProdLimitPageDto();
        pageDto.setProdCode(dto.getProdCode());
        pageDto.setCustomerCode(customerCode);
        pageDto.setMerchantNo(dto.getMerchantNo());
        pageDto.setMerchantName(dto.getMerchantName());
        pageDto.setMerchantType(dto.getMerchantType());
        pageDto.setPageNo(dto.getPageNo());
        pageDto.setPageSize(dto.getPageSize());
        pageDto.setSort("lup.create_time");
        pageDto.setOrder(OrderByEnum.DESC);
        ListResultEx<LimitUserProdVo> limitUserProdVoListResultEx = limitFacade.queryMerchantProdLimitPageList(pageDto);

        return convertListResult(limitUserProdVoListResultEx, limitUserProdVo -> {
            MerchantLimitVo vo = new MerchantLimitVo();
            vo.setLimitNo(limitUserProdVo.getLimitNo());
            vo.setProdCode(limitUserProdVo.getProdCode());
            vo.setCustomerCode(limitUserProdVo.getPUserNo());
            vo.setMerchantNo(limitUserProdVo.getUserNo());
            vo.setMerchantType(ApiEnum.parse(MerchantTypeEnum.class, limitUserProdVo.getUserType()));
            //获取商户名称
            ResultEx<MerchantInfo> merchantInfoEx = merchantInfoService.getMerchantInfo(limitUserProdVo.getPUserNo(), limitUserProdVo.getUserNo());
            if (merchantInfoEx.isSuccess() && ObjectUtil.isNotEmpty(merchantInfoEx.getData())){
                vo.setMerchantName(merchantInfoEx.getData().getMerchantNameCn());
            } else {
                vo.setMerchantName(limitUserProdVo.getUserName());
            }
            vo.setTotalBal(limitUserProdVo.getTotalBal());
            vo.setAvailBal(limitUserProdVo.getAvailBal());
            vo.setUsedBal(limitUserProdVo.getUsedBal());
            vo.setLimitCcy(limitUserProdVo.getLimitCcy());
            return vo;
        });
    }

    @LogAnnotation
    @ApiOperation("获取客户产品额度信息")
    @GetMapping("/cust/{prodCode}")
    public ResultEx<CustomerLimitVo> getCustomerLimit(@ApiParam(name = "产品编码") @PathVariable("prodCode") String prodCode) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        ResultEx<CustomerLimitVo> custProdLimit = limitFacade.getCustProdLimit(customerCode, prodCode);
        CustomerLimitVo customerLimitVo = custProdLimit.getData();
        if (ObjectUtil.isEmpty(customerLimitVo)) {
            return custProdLimit;
        }
        CustomerLimitVo returnCustomerLimitVo = new CustomerLimitVo();
        returnCustomerLimitVo.setLimitNo(customerLimitVo.getLimitNo());
        returnCustomerLimitVo.setProdCode(customerLimitVo.getProdCode());
        returnCustomerLimitVo.setCustomerCode(customerLimitVo.getCustomerCode());
        returnCustomerLimitVo.setCustomerName(customerLimitVo.getCustomerName());
        returnCustomerLimitVo.setTotalBal(customerLimitVo.getTotalBal());
        returnCustomerLimitVo.setAvailBal(customerLimitVo.getAvailBal());
        returnCustomerLimitVo.setUsedBal(customerLimitVo.getUsedBal());
        returnCustomerLimitVo.setLimitCcy(customerLimitVo.getLimitCcy());
        returnCustomerLimitVo.setEffDate(customerLimitVo.getEffDate());
        returnCustomerLimitVo.setExpDate(customerLimitVo.getExpDate());
        returnCustomerLimitVo.setUpdateTime(customerLimitVo.getUpdateTime());

        //过期显示0
        if (LocalDateTime.now().isAfter(customerLimitVo.getExpDate())) {
            returnCustomerLimitVo.setTotalBal(BigDecimal.ZERO);
        }

        return ResultEx.ok(returnCustomerLimitVo);
    }
}
